package cn.com.header.web.controller.common;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * 视图控制器,返回jsp视图给前端
 * 
 * @author StarZou
 * @since 2014年5月28日 下午4:00:49
 **/
@Controller
@RequestMapping("/page")
public class PageController {

    /**
     * 登录页
     */
    @RequestMapping("/toLogin")
    public String toLogin(HttpServletRequest request, HttpServletResponse response) {
        //解决session过期后shiro拦截页面ajax请求失效的问题
        boolean isAjax = false;
        Enumeration<String> values = request.getHeaders("X-Requested-With");
        while(values.hasMoreElements()) {
            String value = values.nextElement();
            if("XMLHttpRequest".equalsIgnoreCase(value)) {
                isAjax = true;
                break;
            }
        }
        if(isAjax) {
            response.setHeader("Session-Status", "timeout");
            System.out.print(request.getRequestURL()+"----"+request.getParameter("kickout"));
            if(request.getParameter("kickout")!=null){//如果是账号在不同地方同时登陆的情况
                response.setHeader("Login-Path", request.getRequestURL()+"?kickout=1");
            }
            else{
                response.setHeader("Login-Path", request.getContextPath());
            }
        }
        return "toLogin";
    }
    /**
     * 登录页
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        //如果已经登录，就直接跳到index
        if(request.getSession().getAttribute("userInfo")!=null){
            return "redirect:/index.do";
        }
        return "login";
    }
    /**
     * 注册
     */
    @RequestMapping("/register")
    public String register(){return "register";}

    /**
     * dashboard页
     */
    @RequestMapping("/dashboard")
    public String dashboard() {
        return "dashboard";
    }

    /**
     * 404页
     */
    @RequestMapping("/404")
    public String error404() {
        return "404";
    }

    /**
     * 401页
     */
    @RequestMapping("/401")
    public String error401() {
        return "401";
    }

    /**
     * 500页
     */
    @RequestMapping("/500")
    public String error500() {
        return "500";
    }

}